<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="licensePlateNumber">
        <el-input
          v-model="queryParams.licensePlateNumber"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车架号" prop="vin">
        <el-input
          v-model="queryParams.vin"
          placeholder="请输入车架号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年检号" prop="annualInspectionNumber">
        <el-input
          v-model="queryParams.annualInspectionNumber"
          placeholder="请输入年检号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年检日期" prop="annualInspectionDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.annualInspectionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择年检日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="年检费用(元)" prop="annualInspectionFee">
        <el-input
          v-model="queryParams.annualInspectionFee"
          placeholder="请输入年检费用(元)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到期日期" prop="expirationDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.expirationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择到期日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="车管所" prop="vehicleAdministrationOffice">
        <el-input
          v-model="queryParams.vehicleAdministrationOffice"
          placeholder="请输入车管所"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登记人员" prop="registerPerson">
        <el-input
          v-model="queryParams.registerPerson"
          placeholder="请输入登记人员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登记日期" prop="registerDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.registerDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择登记日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:annualInspection:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:annualInspection:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:annualInspection:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:annualInspection:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="annualInspectionList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:annualInspection:edit']"
                >修改</el-button>
                <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['system:annualInspection:remove']"
                >删除</el-button>
            </template>
        </el-table-column>
      <el-table-column label="车牌号" align="center" prop="licensePlateNumber" />
      <el-table-column label="车架号" align="center" prop="vin" />
      <el-table-column label="年检号" align="center" prop="annualInspectionNumber" />
      <el-table-column label="年检日期" align="center" prop="annualInspectionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.annualInspectionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年检费用(元)" align="center" prop="annualInspectionFee" />
      <el-table-column label="到期日期" align="center" prop="expirationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车管所" align="center" prop="vehicleAdministrationOffice" />
      <el-table-column label="登记人员" align="center" prop="registerPerson" />
      <el-table-column label="登记日期" align="center" prop="registerDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改年检记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
        <div class="dialogDiv">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-col :span="12">
            <el-form-item label="车牌号" prop="licensePlateNumber">
                <el-input v-model="form.licensePlateNumber" placeholder="请输入车牌号" />
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="车架号" prop="vin">
                <el-input v-model="form.vin" placeholder="请输入车架号" />
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="年检号" prop="annualInspectionNumber">
                <el-input v-model="form.annualInspectionNumber" placeholder="请输入年检号" />
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="年检日期" prop="annualInspectionDate">
                <el-date-picker clearable size="small"
                                style="width: 100%"
                                v-model="form.annualInspectionDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择年检日期">
                </el-date-picker>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="年检费用(元)" prop="annualInspectionFee">
                <el-input v-model="form.annualInspectionFee" placeholder="请输入年检费用(元)" />
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="到期日期" prop="expirationDate">
                <el-date-picker clearable size="small"
                                style="width: 100%"
                                v-model="form.expirationDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择到期日期">
                </el-date-picker>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="车管所" prop="vehicleAdministrationOffice">
                <el-input v-model="form.vehicleAdministrationOffice" placeholder="请输入车管所" />
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="登记人员" prop="registerPerson">
                <el-input v-model="form.registerPerson" placeholder="请输入登记人员" />
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="登记日期" prop="registerDate">
                <el-date-picker clearable size="small"
                                style="width: 100%"
                                v-model="form.registerDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择登记日期">
                </el-date-picker>
            </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
        </el-col>

        <el-col :span="24">
            <el-form-item label="附件">
                <fileUpload v-model="form.attachArea"/>
            </el-form-item>
        </el-col>
      </el-form>
    </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
    .dialogDiv {
        height: 450px;
        padding-right: 20px;
        overflow: auto;
    }
</style>

<script>
import { listAnnualInspection, getAnnualInspection, delAnnualInspection, addAnnualInspection, updateAnnualInspection, exportAnnualInspection } from "@/api/system/annualInspection";

export default {
  name: "AnnualInspection",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 年检记录表格数据
      annualInspectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        licensePlateNumber: null,
        vin: null,
        annualInspectionNumber: null,
        annualInspectionDate: null,
        annualInspectionFee: null,
        expirationDate: null,
        vehicleAdministrationOffice: null,
        registerPerson: null,
        registerDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询年检记录列表 */
    getList() {
      this.loading = true;
      listAnnualInspection(this.queryParams).then(response => {
        this.annualInspectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bindId: null,
        id: null,
        licensePlateNumber: null,
        vin: null,
        annualInspectionNumber: null,
        annualInspectionDate: null,
        annualInspectionFee: null,
        expirationDate: null,
        vehicleAdministrationOffice: null,
        registerPerson: null,
        registerDate: null,
        remark: null,
        attachArea: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加年检记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAnnualInspection(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改年检记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAnnualInspection(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAnnualInspection(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除年检记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAnnualInspection(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有年检记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportAnnualInspection(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
